var util = require('../../utils/util.js')

//获取应用实例
var app = getApp()
Page({
  data: {
    lock_number:'',
    prompt_info:'',
  },

  onLoad: function (option) {
     var that = this;
     //获取到球杆需要还入到几号锁中
     that.setData({lock_number:option.lock_number})
     //that.checkBallArm();
  
  },
  onShow:function(){
    var that = this;

    app.interval_id = setInterval(function () { that.checkBallArm() }, '2000');

    that.checkBallArm();
  },
  //微信小程序切换到后台
  onHide:function(){
    //清除定时器
    clearInterval(app.interval_id);
  },
  checkBallArm:function(){
     this.checkBallArmStatus();
  },
  
  statusPrompt:function(that){

    setTimeout(function () { that.setData({ prompt_info: '正在检测球杆中' }) }, '100');
    setTimeout(function () { that.setData({ prompt_info: '正在检测球杆中·' }) }, '500');
    setTimeout(function () { that.setData({ prompt_info: '正在检测球杆中··' }) }, '1000');
    setTimeout(function () { that.setData({ prompt_info: '正在检测球杆中···' }) }, '1500');
  },
  //验证用户还杆的状态
  checkBallArmStatus:function(){
    var that = this;
    var session_id      = wx.getStorageSync('session_id');
    var ball_arm_number = wx.getStorageSync('ball_arm_number')
    wx.request({
      url  : app.data.servser + '/index/returnBallArm',
      data : {
        session_id      : session_id,
        ball_arm_number : ball_arm_number
      },
      success: function (res) {
        if (res.data.status == 1) {
          wx.removeStorageSync('scan_result');
          wx.removeStorageSync('ball_arm_number');
          //去计算费用
          that.charge(session_id);
        } else {
          if(res.data.status == 0){
            that.statusPrompt(that);
          }else{
            that.setData({
              prompt_info: res.data.error_info,
            });
          }
        }
      }
    })
  },

  //计算消费
  charge: function (session_id) {
    var that = this;
    //var form_id = wx.getStorageSync('form_id');
    var user_feedback = wx.getStorageSync('user_feedback') || 0;
    wx.request({
      url: app.data.servser + '/index/charge',
      data: {
        session_id: session_id,
        //form_id: form_id,
        user_feedback: user_feedback
      },
      success: function (res) {
    
        if (res.data.status == 1) {
          wx.showToast({
            title: '还杆成功',
            icon: 'success',
            duration: 2000
          });
          wx.setStorage({
            key: 'money',
            data: res.data.money
          });
          
        }

        //跳转到首页
        setTimeout(function () {wx.reLaunch({url: '/pages/index/index'})},'1500');}
    })
  },
  
  
})
